- What is a File from an Operating System's perspective?
Answer:
A file is a logical unit of storage created by the OS to store data, programs, or information on secondary storage (like a hard drive or SSD). It is an abstraction that provides a way to store and retrieve data persistently, independent of the physical details of the storage device. To the OS, a file is simply a named collection of related information (bytes) stored on a disk.
- What are the typical attributes of a file?
Answer:
The OS maintains metadata about each file, often stored in a data structure called the file's i-node (in Unix) or Master File Table (MFT) entry (in Windows NTFS). Common attributes include:
- Name: The human-readable name (e.g., report.pdf).
- Identifier: A unique tag (often a number) that identifies the file within the file system.
- Type: The file format (e.g., .txt, .exe, .jpg) which tells the OS and applications how to interpret the data.
- Location: Pointer to where the file is stored on the disk (e.g., block numbers).
- Size: Current file size (in bytes) and possibly a maximum allowed size.
- Protection: Access control information (e.g., read/write/execute permissions for owner, group, others).
- Timestamps: Creation date, last modification date, and last access date.
- What are the basic operations that can be performed on a file?
Answer:
The OS provides a standard set of system calls for file operations:
- Create: Allocate space and create a directory entry for the new file.
- Open: Load the file's metadata into memory so it can be accessed quickly, returning a file descriptor.
- Read: Copy data from the file into a user buffer.
- Write: Copy data from a user buffer into the file.
- Seek: Move the file pointer (the current read/write location) to a specific position within the file.
- Close: Flush any buffered data to disk and release the file descriptor.
- Delete/Unlink: Remove the file's directory entry and release its allocated disk space.
- Truncate: Erase the file's contents but keep its attributes (set size to 0).
- Explain the difference between Sequential Access and Direct (Random) Access.
Answer:
- Sequential Access: Data is read/written in a linear order, one record after another. To access a specific record, you must read all preceding records. The file pointer moves automatically forward. This is the only method for tape drives and is used for applications that process data in a batch (e.g., log files, audio/video streaming).
- Direct (Random) Access: Data can be read or written at any specific location in the file without reading other records. The user provides a block number or offset to the seek() operation. This is essential for databases, indexing systems, and any application that needs to quickly jump to specific data. Modern file systems support direct access on block devices (HDDs/SSDs).
-
What is the purpose of a File Pointer?
Answer:
A file pointer (or file offset) is a per-process variable that indicates the current location (position) within an open file for the next read or write operation.
- When a file is opened, the pointer usually points to the beginning (position 0).
- Each read() or write() operation advances the pointer by the number of bytes read/written.
- A seek() system call explicitly changes the file pointer to a specified location, enabling direct access.
- What is a Directory? What information does it contain?
Answer:
A directory is a special type of file that serves as a container for organizing other files and subdirectories. It provides a logical structure (a hierarchical namespace) to the file system.
A directory entry typically contains:
- The file name.
- A pointer or reference to the file's metadata structure (e.g., the i-node number in Unix).
- (Optionally) File type and basic attributes.
- Explain the difference between Single-Level, Two-Level, and Tree-Structured
Directories.
Answer:
- Single-Level Directory: A single directory containing all files for all users.
- Pros: Very simple.
- Cons: Naming conflicts (no two files can have the same name), difficult to organize, and poor scalability. (Used in early simple systems).
- Two-Level Directory: A master directory for each user, and each user has their own subdirectory.
- Pros: Solves naming conflicts (each user has their own namespace).
- Cons: Users cannot easily group their own files into logical subcategories, and sharing between users is complex.
- Tree-Structured Directory (Hierarchical): Users can create multiple subdirectories within their own directory, forming a tree of arbitrary depth.
- Pros: Extremely flexible, supports logical grouping, and is the standard for modern OSes (Windows, Linux, macOS).
- Cons: Requires more complex path management (absolute vs. relative paths).
- What is the difference between an Absolute Path and a Relative Path?
Answer:
- Absolute Path: The full path from the root directory of the file system (e.g., in Linux: /home/user/Documents/report.txt; in Windows: C:\Users\User\Documents\report.txt). It uniquely identifies a file regardless of the current working directory.
- Relative Path: The path relative to the current working directory (e.g., Documents/report.txt if you are currently in /home/user). It is shorter and more convenient but depends on the user's current location in the file tree.
- What is a "Mount Point" in a file system?
Answer:
A mount point is a directory in an existing file system where an additional file system is attached (mounted). This allows multiple physical storage devices (or partitions) to appear as a single, seamless logical tree.
- Example: On Linux, the root file system is mounted at /. A USB drive might be mounted at /mnt/usb. When you access /mnt/usb, you are actually accessing the root directory of the USB drive's file system. Mounting makes the contents of the external device accessible through the main directory tree.
-
Explain Contiguous Allocation. What are its advantages and disadvantages?
Answer:
Each file occupies a set of contiguous blocks on the disk. The directory entry stores the starting block address and the length (total number of blocks) of the file.
- Advantages:
- Excellent read performance (sequential or random access) because the disk head has minimal movement.
- Simple to implement.
- Disadvantages:
- Suffers from External Fragmentation over time (holes appear as files are deleted).
- Difficult to grow a file dynamically; you need to know the maximum size upfront or move the file to a larger contiguous space.
- Explain Linked Allocation. How does it solve the fragmentation problem?
Answer:
Each file is stored as a linked list of disk blocks, which can be scattered anywhere on the disk. Each block contains a pointer to the next block in the file. The directory entry points to the first and last blocks.
- Fragmentation: Completely eliminates external fragmentation because any free block can be used.
- Disadvantages:
- Poor random access: To access block 'n', you must sequentially traverse the first 'n-1' blocks.
- Reliability: If a pointer in one block is corrupted or lost, the rest of the file is lost.
- Overhead: Each block consumes a small amount of space for the pointer.
- Variation: File Allocation Table (FAT) uses a separate table to hold all pointers, avoiding pointer storage in data blocks.
- Explain Indexed Allocation.
Answer:
Indexed allocation solves the problems of both contiguous and linked allocation. It brings all the pointers for a file together into a single structure called an index block. The index block is an array of disk block addresses. The directory entry points to this index block. To read block 'n', the OS reads the 'n-th' entry from the index block and directly accesses that disk block.
- Advantages: Supports direct access efficiently. No external fragmentation.
- Disadvantages: Wastes space on the index block itself (a fixed overhead). If files are very large, one index block may not be sufficient to store all pointers, leading to multi-level indexing (like Unix inodes with direct, single, double, and triple indirect pointers).
- Given a file system with a block size of 4KB and disk addresses (pointers) of 4 bytes, how large a file can be supported if the index block holds 256 direct pointers?
Answer:
- Number of pointers in one index block = Block Size / Pointer Size = 4096 bytes / 4 bytes = 1024 pointers.
- If the index block holds 1024 direct pointers, the maximum file size = 1024 × 4KB = 4,096 KB = 4 MB.
- (If using a multi-level index, the size becomes massively larger. For example, with single indirect, you get 1024 1024 4KB = 4GB, etc.)
- How does the OS keep track of free disk blocks?
Answer:
The OS maintains a free-space list to track which blocks on the disk are available for allocation. Common methods include:
- Bit Vector (Bitmap): A bit map where each bit represents a disk block. 1 = free, 0 = allocated.
- Pros: Simple and efficient to find free contiguous blocks.
- Cons: Requires a large contiguous chunk of memory.
- Linked List (Free List): All free blocks are linked together with pointers.
- Pros: Simple, uses no large memory.
- Cons: Traversing the list to find free space is slow.
- Grouping: Stores the addresses of multiple free blocks in the first free block. This allows for faster access to a large set of free blocks.
- Counting: Tracks the starting address and length of contiguous free blocks. Useful because multiple contiguous free blocks are common.